<template>
	<view>
		<!-- 注意，如果需要兼容微信小程序，最好通过setRules方法设置rules规则 -->
		<u--form style="margin-top: 10%;margin-left: 30rpx;margin-right: 30rpx;" labelPosition="left"
			:model="repairsRecord" :rules="rules" ref="form1">
			<u-form-item label="报修分类" labelWidth="80" prop="repType" borderBottom ref="item1">
					<uni-data-picker placeholder="请选择分类" popup-title="报修分类" :localdata="typeData"
						v-model="repairsRecord.repType" :map="{text:'name',value:'id',children:'sonType'}" @change="typePickerChange">
					</uni-data-picker>
			</u-form-item>
			<u-form-item label="报修位置" labelWidth="80" prop="repPlace" borderBottom @click="" ref="item1">
					<uni-data-picker placeholder="请选择位置" popup-title="报修位置" :localdata="placeData"
						v-model="repairsRecord.repPlace" :map="{text:'name',value:'id'}" @change="placePickerChange">
					</uni-data-picker>
			</u-form-item>
			<u-form-item label="报修描述" labelWidth="80" prop="repDesc" borderBottom @click="" ref="item1">
				<u--textarea :confirmType="null" placeholder="不低于3个字" v-model="repairsRecord.repDesc" count
					maxlength="100"></u--textarea>
			</u-form-item>
		</u--form>
		<u-button type="primary" text="提交" customStyle="margin-top: 20%;width: 30%" @click="submit"></u-button>
		<u-button type="error" text="重置" customStyle="margin-top: 10px;width: 30%" @click="reset"></u-button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showTypePicker: false,
				rules: {
					'repDesc': {
						type: 'string',
						required: true,
						min: 3,
						max: 100,
						message: '请填写描述',
						trigger: ['blur', 'change']
					},
				},
				switchVal: false,
				repairsRecord: {
					repType: "",
					repPlace: "",
					repDesc: "",
					person: "",
					pePhone: "",
					repPlaceIds: [],
				},
				typeData: [],
				placeData: [],
			};
		},
		onLoad() {
			this.getPlaceData();
			this.getTypeData();
		},
		methods: {
			getPlaceData() {
				this.$axios.post("/bx/place/list").then((r) => {
					let placeList = JSON.parse(r.data.data);
					let data = [];
					for (let p of placeList) {
						p["children"] = p.sonPlace;
						if (p.parentId == 0) {
							data.push(p);
						}
					}
					this.wrapPlaceData(data, placeList);
					this.placeData = data;
					for (let p of this.placeData) {
						this.alterPlaceData(p);
					}
				});
			},
			getTypeData() {
				this.$axios.post("/bx/type/list").then((r) => {
					let typeList = JSON.parse(r.data.data);
					let data = [];
					for (const type of typeList) {
						type["children"] = type.sonType;
						if (type.parentId == 0) {
							data.push(type);
						}
					}
					this.wrapTypeData(data, typeList);
					this.typeData = data;
					for (let type of this.typeData) {
						this.alterTypeData(type);
					}
				});
			},
			wrapTypeData(data, typeList) {
				for (const type of data) {
					let sonType = [];
					for (const t of typeList) {
						if (t.parentId == type.id) {
							sonType.push(t);
						}
					}
					this.wrapTypeData(sonType, typeList)
					type.children = sonType;
				}
			},
			alterTypeData(type) {
				if (type.sonType == null) {
					type.sonType = undefined;
					return;
				}
				for (const t of type.sonType) {
					this.alterTypeData(t);
				}
			},
			wrapPlaceData(data, placeList) {
				for (const place of data) {
					let sonPlace = [];
					for (const p of placeList) {
						if (p.parentId == place.id) {
							sonPlace.push(p);
						}
					}
					this.wrapPlaceData(sonPlace, placeList)
					place.children = sonPlace;
				}
			},
			alterPlaceData(place) {
				if (place.sonPlace == null) {
					place.sonPlace = undefined;
					return;
				}
				for (const p of place.sonPlace) {
					this.alterPlaceData(p);
				}
			},
			placePickerChange(e) {
				const value = e.detail.value;
				let ids = [];
				let place = "";
				for (let i of value) {
					ids.push(i.value)
					place += i.text + "/";
				}
				this.repairsRecord.repPlaceIds = ids;
				this.repairsRecord.repPlace = place.substring(0, place.length - 1)
			},
			typePickerChange(e) {
				const value = e.detail.value;
				let type = "";
				for (let i of value) {
					type += i.text + "/";
				}
				this.repairsRecord.repType = type.substring(0, type.length - 1)
			},
			submit() {
				if (this.repairsRecord.repType == "" || this.repairsRecord.repPlace == "") {
					uni.showToast({
						title: '填写参数有误',
						icon: 'fail'
					});
					return;
				}
				this.$refs.form1.validate().then(res => {
					this.$axios.post("/bx/record/add", this.repairsRecord).then(r => {
						if (r.data.code == 200) {
							uni.showToast({
								title: '报修成功',
								icon: 'success'
							});
								uni.switchTab({
									url: "/pages/my/my"
								})
						
						}
					});
				}).catch(errors => {
					uni.showToast({
						title: '填写参数有误',
						icon: 'fail'
					});
				})
			}
		},
	};
</script>

<style lang="scss">

</style>
